<?php

use think\Container;


if (!function_exists('addons_path')) {

    function addons_path($addon = '')
    {
        return app()->getRootPath() . 'addons' . DIRECTORY_SEPARATOR . $addon;
    }
}

if (!function_exists('getBuilderConfig')) {

    function getBuilderConfig($key = '')
    {
        $config = json_decode(file_get_contents(addons_path('BuilderPlus') . DIRECTORY_SEPARATOR . 'config.json'), true);
        return $key ? $config[$key] : $config;
    }
}

if (!function_exists('setBuilderConfig')) {

    function setBuilderConfig($data)
    {
        $path = addons_path('BuilderPlus') . DIRECTORY_SEPARATOR . 'config.json';
        $config = json_decode(file_get_contents($path), true);
        foreach ($data as $key => $item) {
            $config[$key] = $item;
        }
        file_put_contents($path, json_encode($config));
    }
}

if (!function_exists('builderView')) {

    /**
     * 获取BuilderView对象
     * @return \addons\BuilderPlus\driver\View
     */
    function builderView()
    {
        return Container::getInstance()->make(addons\BuilderPlus\driver\View::class);
    }
}

if (!function_exists('builderFormInit')) {


    function builderFormInit($options)
    {
        return builderView()->builderFormInit($options);
    }
}

if (!function_exists('getAttachMenu')) {

    function getAttachMenu()
    {
        $menu = [];
        if (env('APP_DEBUG')) {
            $menu[] = [
                'title'     => '定时任务',
                'sub_title' => '定时',
                'css'       => 'layui-icon-log',
                'child'     => [
                    [
                        'href'  => url('crontab/index')->build(),
                        'title' => '任务列表'
                    ]
                ]
            ];
        }

        return $menu;
    }
}
